package com.obtk.shilipai.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.shilipai.entity.Dishe;
import com.obtk.shilipai.entity.DisheType;
import com.obtk.shilipai.entity.Shop;
import com.obtk.shilipai.service.DisheService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/dishe")
public class DisheController {
    @Autowired
    private DisheService disheService;

    @Autowired
    private HttpServletRequest request;

    /**
     *
     * @param pageNo 查询页码
     * @param pageSize  每页显示的行数
     * @param model
     * @return
     */
    @RequestMapping(value = "/getDisheByAll")
    @ApiOperation(value = "获取所有用户信息")
    public String getDisheByAll(@RequestParam(required = false, defaultValue = "1") int pageNo,
                         @RequestParam(required = false, defaultValue = "10") int pageSize,
                         Model model, Integer shopId,
                                @RequestParam(required = false, defaultValue = "0") Integer disheTypeId,
                                @RequestParam(required = false, defaultValue = "0") Integer status,
                                @RequestParam(required = false, defaultValue = "") String keywords) {
        System.out.println("shopId"+shopId);
        List<DisheType> disheType1 = disheService.getDisheNum();
        for (int i=0;i<disheType1.size();i++){
                disheService.setDisheNum(disheType1.get(i).getDisheTypeId(),disheType1.get(i).getDisheNum());
        }
        List<DisheType> disheType = disheService.getDisheType(shopId);


        //分页第一步：初始化参数  pageNo：开始页码  pageSize：每页显示行数
        PageHelper.startPage(pageNo, pageSize);
        //分页第二步：查询结果集
        List<Dishe> dishe = disheService.getDisheByAll(shopId,disheTypeId,status,keywords);

        //第三步：封装为PageInfo对象
        PageInfo<Dishe> page = new PageInfo<Dishe>(dishe);
        model.addAttribute("page",page);
        model.addAttribute("shopId",shopId);
        model.addAttribute("disheType",disheType);
        return "dishe/list";
    }


    @RequestMapping("addDisheType")
    @ResponseBody
    public String addDisheType(DisheType disheType){
        System.out.println(disheType);

        return disheService.addDisheType(disheType).toString();

    }

    @RequestMapping("addDishe")
    @ResponseBody
    public String addDishe(@RequestParam(value = "file") MultipartFile file,@ModelAttribute("dishe") Dishe dishe) throws IOException {
        HttpSession httpSession=request.getSession();
        String shopName = (String)httpSession.getAttribute("shopName");
        System.out.println(shopName);
        String originalFilename = file.getOriginalFilename();
            // 拼接保存文件的完整路径
        String filePath = "C:/Users/T/Desktop/shilipai/src/main/resources/static/dishe/"+shopName+"/" + originalFilename;
        File dest = new File(filePath);
        if (!dest.exists()) {
            // 保存文件到指定路径
            file.transferTo(new File(filePath));
        }else {
            System.out.println("文件已存在");
            // 文件已存在，重命名文件名
//            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//            String newFilename = originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "_" + System.currentTimeMillis() + suffix;
//            filePath = "C:/Users/T/Desktop/shilipai/src/main/resources/static/dishe/" + newFilename;
//            dishe.setDisheImage(newFilename);
//            dest = new File(filePath);
//            file.transferTo(dest);
        }
        System.out.println(dishe);
        return disheService.addDishe(dishe).toString();

    }

    @RequestMapping("updateDishe")
    @ResponseBody
    public String updateDishe(@RequestParam(value = "file",required = false) MultipartFile file,@ModelAttribute("dishe")Dishe dishe,@RequestParam("typeId")String typeId) throws IOException {
       if (file!=null){
           HttpSession httpSession=request.getSession();
           String shopName = (String)httpSession.getAttribute("shopName");
           String originalFilename = file.getOriginalFilename();
           // 拼接保存文件的完整路径
           String filePath = "C:/Users/T/Desktop/shilipai/src/main/resources/static/dishe/"+shopName+"/" + originalFilename;
           File dest = new File(filePath);
           if (!dest.exists()) {
               // 保存文件到指定路径
               file.transferTo(new File(filePath));
           }else {
               System.out.println("文件已存在");
           }
       }
        // 保存文件到指定路径
        String s = disheService.updateDishe(dishe).toString();
        System.out.println("dishe---"+dishe);
        System.out.println(s);
        return s;

    }

    @RequestMapping("delDisheType")
    @ResponseBody
    public String delDisheType(String idStr){
        String[] idArray = idStr.split(",");
        System.out.println(idStr);
        Integer flag = disheService.delDisheType(idArray);
        return flag.toString();

    }
    //删除商品信息 单个删除和多个删除一个方法
    @RequestMapping("delDishe")
    @ResponseBody
    public String del(@RequestParam("idStr") String idStr,@RequestParam("typeIds") String typeIds){
        String[] idArray = idStr.split(",");
        String[] typeidArray = typeIds.split(",");
        System.out.println(idStr);
        System.out.println("长度"+typeidArray.length);
        Integer flag = disheService.delDishe(idArray);
        return flag.toString();

    }

    @RequestMapping("getDisheById")
    @ResponseBody
    public Dishe getDisheById(Integer disheId){
        Dishe dishe = disheService.getDisheById(disheId);
        System.out.println(dishe);
        return dishe;
    }
    @RequestMapping("xiajiaDishe")
    @ResponseBody
    public String xiajiaDishe(@RequestParam("idStr") String idStr,@RequestParam("typeIds") String typeIds){
        System.out.println(idStr+"+++++++++++"+typeIds);
        String[] idArray = idStr.split(",");
        String[] typeidArray = typeIds.split(",");
        System.out.println(typeidArray[0]+"-------"+typeidArray.length);
        Integer flag = disheService.xiajiaDishe(idArray);
        return flag.toString();
    }

    @RequestMapping("shangjiaDishe")
    @ResponseBody
    public String shangjiaDishe(@RequestParam("idStr") String idStr,@RequestParam("typeIds") String typeIds){
        System.out.println(idStr+"+++++++++++"+typeIds);
        String[] idArray = idStr.split(",");
        String[] typeidArray = typeIds.split(",");
        Integer flag = disheService.shangjiaDishe(idArray);
        return flag.toString();
    }

}
